//
//  WeatherHTTPClient.m
//  Weather
//
//  Created by cygarm on 15/9/29.
//  Copyright © 2015年 Scott Sherwood. All rights reserved.
//

#import "WeatherHTTPClient.h"

static NSString * const WorldWeatherOnlineAPIKey = @"f97fe8422d16940ccb1286d6a938e";
static NSString * const WorldWeatherOnlineURLString = @"https://api.worldweatheronline.com/free/v2/";

@implementation WeatherHTTPClient

+ (WeatherHTTPClient *)sharedWeatherHTTPClient
{
	static WeatherHTTPClient *_sharedWeatherHTTPClient = nil;
	
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		_sharedWeatherHTTPClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:WorldWeatherOnlineURLString]];
	});
	
	return _sharedWeatherHTTPClient;
}

- (instancetype)initWithBaseURL:(NSURL *)url
{
	self = [super initWithBaseURL:url];
	
	if (self) {
		self.responseSerializer = [AFJSONResponseSerializer serializer];
		self.requestSerializer = [AFJSONRequestSerializer serializer];
	}
	
	return self;
}

- (void)updateWeatherAtLocation:(CLLocation *)location forNumberOfDays:(NSUInteger)number
{
	NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
	
	parameters[@"num_of_days"] = @(number);
	parameters[@"q"] = [NSString stringWithFormat:@"%f,%f", location.coordinate.latitude, location.coordinate.longitude];
	parameters[@"format"] = @"json";
	parameters[@"key"] = WorldWeatherOnlineAPIKey;
	
	[self GET:@"weather.ashx" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
		if ([self.delegate respondsToSelector:@selector(weatherHTTPClient:didUpdateWithWeather:)]) {
			[self.delegate weatherHTTPClient:self didUpdateWithWeather:responseObject];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		if ([self.delegate respondsToSelector:@selector(weatherHTTPClient:didFailWithError:)]) {
			[self.delegate weatherHTTPClient:self didFailWithError:error];
		}
	}];
}

@end
